﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;

namespace InsuranceAdjuster.OccasionallyConnected.Messages {
    public class GetVehiclePhotoMessage : Message {
        public int VehicleYear {
            get { return GetMessageData<int>("VehicleYear"); }
            set { SetMessageData<int>("VehicleYear", value); }
        }
        public string VehicleMake {
            get { return GetMessageData<string>("VehicleMake"); }
            set { SetMessageData<string>("VehicleMake", value); }
        }
        public string VehicleModel {
            get { return GetMessageData<string>("VehicleModel"); }
            set { SetMessageData<string>("VehicleModel", value); }
        }
        public Guid PhotoGuid {
            get { return GetMessageData<Guid>("PhotoGuid"); }
            set { SetMessageData<Guid>("PhotoGuid", value); }
        }

        public GetVehiclePhotoMessage() {
        }
        public GetVehiclePhotoMessage(int vehicleYear, string vehicleMake, string vehicleModel, Guid photoGuid) {
            CacheRules = MessageDataSource.CacheAndServer | MessageDataSource.FromServerOnlyIfNoCacheOption;

            VehicleYear = vehicleYear;
            VehicleMake = vehicleMake;
            VehicleModel = vehicleModel;
            PhotoGuid = photoGuid;

            MessageName = String.Format("{0} {1} {2} Photo", VehicleYear, VehicleMake, VehicleModel);
        }

        protected override void OnlineOperation() {
            MarkAsyncStarted();
            GetDataService().GetReferencePhoto((Guid)this.PhotoGuid, vehicleReferencePhoto =>
            {
                if (vehicleReferencePhoto.Any())
                    ResponseEntities.Add("Photo", new MessageResult(Verb.Read, Origin.Server, vehicleReferencePhoto[0]));

                MarkAsyncFinished();
            });
        }
        protected override List<MessageResult> OfflineOperation(IObjectStore objectStore) {
            var Output = new List<MessageResult>();

            if (objectStore.ContainsEntity(PhotoGuid))
                Output.Add(new MessageResult(Verb.Read, Origin.Cache, objectStore.GetEntity<VehicleReferencePhoto>(PhotoGuid)));

            return Output;
        }
        protected override List<MessageResult> CompileServerResults() {

            return this.ResponseEntities.Select(x => x.Value).ToList();
        }

    }
}
